package com.bbjob.service.impl;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.bbjob.client.result.ResultForExamineeList;
import com.bbjob.dto.Result;
import com.bbjob.mapper.DeliveryExamineeInfoMapper;
import com.bbjob.model.DeliveryExamineeInfo;
import com.bbjob.model.ExamineeInfo;
import com.bbjob.query.QueryExaminee;
import com.bbjob.service.IExamineeInfoService;
import com.bbjob.service.ISystemService;
import com.bbjob.toolutil.HttpClientUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

@Service
public class ExamineeInfoServiceImpl implements IExamineeInfoService{

	@Value("${sys.uniquecode}")
	private String uniquecode;
	@Value("${examol.center.url}")
	private String centerUrl;
	@Value("${examol.center.examinee.entry.url}")
	private String centerExamineeEntry;
	@Value("${examol.center.examinee.list.url}")
	private String centerExamineeList;
	@Value("${examol.center.examinee.url}")
	private String centerExaminee;
	@Value("${examol.center.examinee.edit.url}")
	private String centerExamineeEdit;
	@Value("${examol.center.examinee.del.url}")
	private String centerExamineeDel;	
	@Value("${examol.center.stuAll}")
	private String centerExamineeAll;
	@Resource
	private DeliveryExamineeInfoMapper deliveryExamineeInfoMapper;
	@Resource
	private ISystemService systemService;
	@Override
	public Result entryInfo(ExamineeInfo examineeInfo) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uniquecode", uniquecode);
		DeliveryExamineeInfo deliveryExaminee = new DeliveryExamineeInfo();
		
		try {
			if (examineeInfo.geteId() != null) {
				deliveryExaminee.seteId(examineeInfo.geteId());
				params.put("eId", examineeInfo.geteId());
			}

			if (examineeInfo.getsNo() != null && !"".equals(examineeInfo.getsNo())) {
				deliveryExaminee.setsNo(examineeInfo.getsNo());
				params.put("sNo", examineeInfo.getsNo());
			}
			
			if (examineeInfo.geteNo() != null && !"".equals(examineeInfo.geteNo())) {
				deliveryExaminee.seteNo(examineeInfo.geteNo());
				params.put("eNo", examineeInfo.geteNo());
			}
			
			if (examineeInfo.getField1() != null && !"".equals(examineeInfo.getField1())) {
				deliveryExaminee.setField1(examineeInfo.getField1());
				params.put("field1", examineeInfo.getField1());
			}
			
			Result testConn = systemService.TestConn();
			
			if (testConn.getCode() == 200) {
				String resultStr = HttpClientUtil.doPost(centerUrl + centerExamineeEntry, params);
				
			    GsonBuilder builder = new GsonBuilder();
			    
			    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
			    	@Override   
			    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
			    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
			    	}
			    });
				
				Result result = builder.create().fromJson(resultStr, Result.class);
				
				// 同步考生信息
				params.put("id", examineeInfo.geteId());
				params.put("uniquecode", uniquecode);
				String examineeStr = HttpClientUtil.doPost(centerUrl + centerExamineeAll, params);
				
				ResultForExamineeList examinees = builder.create().fromJson(examineeStr, ResultForExamineeList.class);
				
				if (examinees.getCode() == 200) {
					List<DeliveryExamineeInfo> list = examinees.getData();
					for (DeliveryExamineeInfo info : list) {
						DeliveryExamineeInfo dExamineeInfo = deliveryExamineeInfoMapper.selectByPrimaryKey(info.getId());
						if (dExamineeInfo != null) {
							deliveryExamineeInfoMapper.updateByPrimaryKey(dExamineeInfo);
						} else {
							deliveryExamineeInfoMapper.insert(info);
						}
					}
				}
				
				
				return result;
			} else {
				return new Result(8106, "与中心断网了");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	@Override
	public Result selectByParams(QueryExaminee examinee) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		
		try {
			params.put("uniquecode", uniquecode);
			params.put("pageNum", examinee.getPageNum());
			params.put("pageSize", examinee.getPageSize());
			
			if (examinee.getSno() != null && !"".equals(examinee.getSno())) {
				params.put("sno", examinee.getSno());
			}

			if (examinee.getSname() != null && !"".equals(examinee.getSname())) {
				params.put("sname", examinee.getSname());
			}
			
			if (examinee.geteId() != null) {
				params.put("eId", examinee.geteId());
			}
			
			String resultStr = HttpClientUtil.doPost(centerUrl + centerExamineeList, params);
			
			Result list = new Gson().fromJson(resultStr, Result.class);
			
			return list;
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	@Override
	public Result selectByPrimaryKey(Integer id) {
		
		try {
			
			String resultStr = HttpClientUtil.doGet(centerUrl + centerExaminee + id);
			
		    GsonBuilder builder = new GsonBuilder();
		    
		    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
		    
			Result result = builder.create().fromJson(resultStr, Result.class);
			
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	@Override
	public Result updateStudent(ExamineeInfo examineeInfo, Integer id) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			if (examineeInfo.geteId() != null) {
				params.put("eId", examineeInfo.geteId());
			}

			if (examineeInfo.getsNo() != null && !"".equals(examineeInfo.getsNo())) {
				params.put("sNo", examineeInfo.getsNo());
			}
			
			if (examineeInfo.geteNo() != null && !"".equals(examineeInfo.geteNo())) {
				params.put("eNo", examineeInfo.geteNo());
			}
			
			if (examineeInfo.getField1() != null && !"".equals(examineeInfo.getField1())) {
				params.put("field1", examineeInfo.getField1());
			}
			
			String resultStr = HttpClientUtil.doPost(centerUrl + centerExamineeEdit + id, params);
			
		    GsonBuilder builder = new GsonBuilder();
		    
		    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
		    DeliveryExamineeInfo updateInfo = deliveryExamineeInfoMapper.selectByPrimaryKey(id);
		    if (updateInfo != null) {
		    	if (examineeInfo.getsNo() != null && !"".equals(examineeInfo.getsNo())) {
		    		updateInfo.setsNo(examineeInfo.getsNo());
				}
				
				if (examineeInfo.geteNo() != null && !"".equals(examineeInfo.geteNo())) {
					updateInfo.seteNo(examineeInfo.geteNo());
				}
				
				if (examineeInfo.getField1() != null && !"".equals(examineeInfo.getField1())) {
					updateInfo.setField1(examineeInfo.getField1());
				}
				deliveryExamineeInfoMapper.updateByPrimaryKey(updateInfo);
		    }
		    		    
			Result result = builder.create().fromJson(resultStr, Result.class);
			
			return result;
		} catch(Exception e) {
			e.printStackTrace();
			return new Result(8106, "出错了");
		}
	}
	@Override
	public void delById(Integer id) {
		try {
			String resultStr = HttpClientUtil.doGet(centerUrl + centerExamineeDel + id);
			
		} catch(Exception e) {
			e.printStackTrace();
		}
		
	}
}
